package com.night.utils;

/**
 * @Author: chen yang
 * @Date: 2024/12/12 22:40
 */
public class StringUtils {

    /**
     * 下划线风格转换为小驼峰风格
     * example_string_to_convert -> exampleStringToConvert
     */
    public static String toLowerCamelCase(String underScoreString) {
        if (underScoreString == null || underScoreString.isEmpty()) {
            return underScoreString;
        }

        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = false;

        for (int i = 0; i < underScoreString.length(); i++) {
            char ch = underScoreString.charAt(i);

            if (ch == '_') {
                nextUpperCase = true;
            } else if (nextUpperCase) {
                result.append(Character.toUpperCase(ch));
                nextUpperCase = false;
            } else {
                result.append(Character.toLowerCase(ch));
            }
        }

        return result.toString();
    }


    /**
     * 将下划线风格转换为大驼峰风格
     * example_string_to_convert -> ExampleStringToConvert
     */
    public static String toUpperCamelCase(String underScoreString) {
        String lowerCamelCase = toLowerCamelCase(underScoreString);
        if (lowerCamelCase == null || lowerCamelCase.isEmpty()) {
            return lowerCamelCase;
        }

        return Character.toUpperCase(lowerCamelCase.charAt(0)) + lowerCamelCase.substring(1);
    }
}
